<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\Admin;

class Shop extends Admin
{
    /**
     * 店铺列表
     * @return [html] [页面内容]
     * @author molong <ycgpp@126.com>
     */
    public function index()
    {
        $user = session('user_auth');
        $order = "id desc";
        $map = $this->buildMap();
        $list = db('Shop')->where($map)->order($order)->paginate(20);
        $data = array(
            'list' => $list,
            'page' => $list->render(),
        );
        $template = 'shop/index';
        $this->assign($data);
        $this->setMeta("店铺列表");
        return $this->fetch($template);
    }

    /**
     * 增加店铺
     * @author molong <ycgpp@126.com>
     */
    public function add($id)
    {
        $user = session('user_auth');
        if (IS_POST) {
            $stock = $_POST['stock'];
            $result = db('Product')->where(array('id' => $id))->setInc('stock', $stock);
            if ($result !== false) {
                //记录行为
                $dat['type'] = 1;
                $dat['content'] = $user['username'] . '在' . date('Y-m-d H:i:s', time()) . '增加库存' . $stock;
                $dat['user_id'] = $user['uid'];
                $dat['pid'] = $id;
                $dat['bid'] = $user['pid'];
                $dat['add_time'] = time();
                $dat['amount'] = $_POST['stock'];
                $dat['price'] = number_format($_POST['price'], 2);
                $dat['total_price'] = number_format(($_POST['stock'] * $_POST['price']), 2);
                db('inventory_operation')->insertGetId($dat);
                action_log('add_stock', 'pro', $result, session('user_auth.uid'));
                return $this->success("成功！", url('admin/pro/index'));
            } else {
                return $this->error($this->model->getError(), url('admin/pro/edit', array('id' => $id)));
            }
        } else {
            $data = array(
                'info' => array(),
            );
            $template = 'shop/edit';
            $this->assign($data);
            $this->setMeta("添加");
            return $this->fetch($template);
        }
    }


    /**
     * 库存减少
     * @author molong <ycgpp@126.com>
     */
    public function reduce($id)
    {
        $user = session('user_auth');
        if (IS_POST) {
            $stock = $_POST['stock'];
            $result = db('Product')->where(array('id' => $id))->setDec('stock', $stock);
            if ($result !== false) {
                //记录行为
                $dat['type'] = 2;
                $dat['content'] = $user['username'] . '在' . date('Y-m-d H:i:s', time()) . '减少库存' . $stock;
                $dat['user_id'] = $user['uid'];
                $dat['pid'] = $id;
                $dat['bid'] = $user['pid'];
                $dat['add_time'] = time();
                $dat['amount'] = $_POST['stock'];
                $dat['price'] = number_format($_POST['price'], 2);
                $dat['total_price'] = number_format(($_POST['stock'] * $_POST['price']), 2);
                db('inventory_operation')->insertGetId($dat);
                action_log('reduce_stock', 'pro', $result, session('user_auth.uid'));
                return $this->success("出库成功！", url('admin/pro/index_reduce'));
            } else {
                return $this->error($this->model->getError(), url('admin/pro/edit', array('id' => $id)));
            }
        } else {
            if (!$id) {
                return $this->error("非法操作！");
            }
            $info = db('Product')->find($id);
            $info['operate'] = 'reduce';
            if (!$info) {
                return $this->error($this->model->getError());
            }
            $data = array(
                'info' => $info,
            );
            $template = 'product/edit';
            $this->assign($data);
            $this->setMeta("商品出库");
            return $this->fetch($template);
        }
    }


    /**
     * 库存操作记录
     * @author molong <ycgpp@126.com>
     */
    public function stock_record()
    {
        $user = session('user_auth');
        $order = "id desc";
        $map = $this->buildMap();
        $mpro['status'] = 1;
        if ($user['is_shop'] == 1) {
            $mpro['bid'] = $map['bid'] = $user['uid'];
        }
        if ($user['is_shop'] == 2) {
            $mpro['uid'] = $map['uid'] = $user['uid'];
        }
        $pro = db('Product')->field('id,title')->where($mpro)->order($order)->select();
        $list = db('inventory_operation')->where($map)->order($order)->paginate(20);
        foreach ($list as $k => $v) {
            $v['time'] = date('Y-m-d', $v['add_time']);
            $v['typename'] = '';
            if ($v['type'] == 1) {
                $v['typename'] = '增加库存';
            }
            if ($v['type'] == 2) {
                $v['typename'] = '减少库存';
            }
            foreach ($pro as $kp => $vp) {
                if ($v['pid'] == $vp['id']) {
                    $v['title'] = $vp['title'];
                } else {
                    $v['title'] = '';
                }
            }
            $lists[] = $v;
        }
        $data = array(
            'list' => $lists,
            'page' => $list->render(),
        );
        $template = 'product/stock';
        $this->assign($data);
        $this->setMeta("库存操作记录");
        return $this->fetch($template);
    }

    /**
     * 盘点
     * @return [html] [页面内容]
     * @author molong <ycgpp@126.com>
     */
    public function stocking()
    {
        $user = session('user_auth');
        $order = "id desc";
        $map = $this->buildMap();
        if ($user['is_shop'] == 1) {
            $map['bid'] = $user['uid'];
        }
        if ($user['is_shop'] == 2) {
            $map['uid'] = $user['uid'];
        }
        $time = strtotime(date('Y-m-d 00:00:00'));
        $map['inventory_time'] = array('lt', $time);
        $list = db('Product')->where($map)->order($order)->paginate(20);
        $data = array(
            'operate' => '',
            'list' => $list,
            'page' => $list->render(),
        );
        $template = 'product/index';
        $this->assign($data);
        $this->setMeta("盘点列表");
        return $this->fetch($template);
    }


    /**
     * 盘点
     * @author molong <ycgpp@126.com>
     */
    public function inventory($id)
    {
        $user = session('user_auth');
        if (IS_POST) {
            $data['inventory'] = $_POST['amount'];
            $data['inventory_id'] = $user['uid'];
            $data['inventory_time'] = time();
            $result = db('Product')->where(array('id' => $id))->update($data);
            if ($result !== false) {
                //记录行为
                $dat['type'] = 3;
                $dat['content'] = $user['username'] . '在' . date('Y-m-d H:i:s', time()) . '进行盘点';
                $dat['user_id'] = $user['uid'];
                $dat['pid'] = $id;
                $dat['bid'] = $user['pid'];
                $dat['amount'] = $_POST['amount'];
                $dat['add_time'] = time();
                db('stocking')->insertGetId($dat);
                action_log('inventoty', 'pro', $result, session('user_auth.uid'));
                return $this->success("盘点成功！", url('admin/pro/stocking'));
            } else {
                return $this->error($this->model->getError(), url('admin/pro/inventory', array('id' => $id)));
            }
        } else {
            if (!$id) {
                return $this->error("非法操作！");
            }
            $info = db('Product')->find($id);
            if (!$info) {
                return $this->error($this->model->getError());
            }
            $data = array(
                'info' => $info,
            );
            $template = 'product/inventory';
            $this->assign($data);
            $this->setMeta("商品盘点");
            return $this->fetch($template);
        }
    }


    /**
     * 盘点操作记录
     * @author molong <ycgpp@126.com>
     */
    public function inventory_record()
    {
        $user = session('user_auth');
        $order = "id desc";
        $map = $this->buildMap();
        $mpro['status'] = 1;
        if ($user['is_shop'] == 1) {
            $mpro['bid'] = $map['bid'] = $user['uid'];
        }
        if ($user['is_shop'] == 2) {
            $mpro['uid'] = $map['uid'] = $user['uid'];
        }
        $pro = db('Product')->field('id,title')->where($mpro)->order($order)->select();
        $list = db('stocking')->where($map)->order($order)->paginate(20);
        foreach ($list as $k => $v) {
            $v['time'] = date('Y-m-d', $v['add_time']);
            foreach ($pro as $kp => $vp) {
                if ($v['pid'] == $vp['id']) {
                    $v['title'] = $vp['title'];
                } else {
                    $v['title'] = '';
                }
            }
            $lists[] = $v;
        }
        $data = array(
            'list' => $lists,
            'page' => $list->render(),
        );
        $template = 'product/inventory_record';
        $this->assign($data);
        $this->setMeta("盘点记录");
        return $this->fetch($template);
    }


    /**
     * 创建搜索
     * @return [array] [查询条件]
     */
    protected function buildMap()
    {
        $map = array();
        $data = $this->request->param();
        foreach ($data as $key => $value) {
            if ($value) {
                if ($key == 'keyword') {
                    $map['title'] = array("LIKE", "%$value%");
                } elseif ($key == 'category') {
                    $map['category_id'] = $value;
                } elseif ($key == 'create_time') {
                    $map['create_time'] = array('BETWEEN', array(strtotime($value[0]), strtotime($value[1])));
                } else {
                    $map[$key] = $value;
                }
            }
        }
        if (isset($map['page'])) {
            unset($map['page']);
        }
        if (isset($map['model_id'])) {
            unset($map['model_id']);
        }
        $this->assign($data);
        return $map;
    }

}